package myProject.gwt.client.simpleTable;

import java.util.ArrayList;
import java.util.List;

import myProject.gwt.client.DebugPanel;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class AbstractHeader extends Composite implements IHeader{

	private HorizontalPanel horizontalPanel = new HorizontalPanel();
	private IHeaderListener listener;
	//private ISizer activeSizer = null;
	private IHeaderCell activeSizer = null;
	private int mouseX;
	private int oldColumnWidth;
	private List<IHeaderCell> headerCellList = new ArrayList<IHeaderCell>();

	
	public AbstractHeader(){
		initWidget(horizontalPanel);
		setStylePrimaryName("header");
		
		Event.addNativePreviewHandler(new NativePreviewHandler(){

			@Override
			public void onPreviewNativeEvent(NativePreviewEvent event) {

				if(activeSizer != null) event.cancel();
				NativeEvent e = event.getNativeEvent();
				if(e == null) return;
					
				if(activeSizer != null && event.getTypeInt() == Event.ONMOUSEMOVE){															
					int delta = e.getScreenX() - mouseX;					
					IHeaderCell cell = getHeaderCell(activeSizer.getColumnIndex());					

					cell.setWidth(oldColumnWidth+delta);					
					//DebugPanel.addText(cell.getWidth()+"");
					//DebugPanel.addText(cell.getColumnIndex()+"");
					if(listener != null) listener.headerResizing(cell);
					event.cancel();
				}
				else if(activeSizer == null && event.getTypeInt() == Event.ONMOUSEDOWN){					
					//Window.alert("down");
					mouseX = e.getScreenX();
					
				}
				else if(activeSizer != null && event.getTypeInt() == Event.ONMOUSEUP){					
					event.cancel();
					IHeaderCell headerCell = getHeaderCell(activeSizer.getColumnIndex());
					activeSizer = null;
					if(listener != null) listener.headerResized(headerCell);										
				}								
			}
			
		});
	}
	
	/*@Override
	public void addHeaderCell(IHeaderCell headerCell) {
		horizontalPanel.add(headerCell.getComponent());
		int columnIndex = headerCellList.size();
		final ISizer sizer = createSizer(columnIndex);
		horizontalPanel.add(sizer.getComponent());
		
		sizer.setListener(new ISizerListener(){

			@Override
			public void MouseDown() {
				activeSizer = sizer;
				oldColumnWidth = getHeaderCell(activeSizer.getColumnIndex()).getWidth();
				DebugPanel.addText(sizer.getColumnIndex()+"");
			}
			
		});
		
		headerCellList.add(headerCell);
	}*/
	
	@Override
	public void addHeaderCell(final IHeaderCell headerCell) {
		horizontalPanel.add(headerCell.getComponent());
		headerCell.getComponent().addStyleName("headerCell");
		int columnIndex = headerCellList.size();

		headerCell.addMouseMoveHandler(new MouseMoveHandler(){
			@Override
			public void onMouseMove(MouseMoveEvent event) {
				if(event.getX() > headerCell.getComponent().getOffsetWidth()-10) {				
					headerCell.getComponent().getElement().getStyle().setProperty("cursor", "w-resize");
				}else{
					headerCell.getComponent().getElement().getStyle().setProperty("cursor", "");
				}
			}			
		});
		
		headerCell.addMouseDownHandler(new MouseDownHandler(){
			@Override
			public void onMouseDown(MouseDownEvent event) {
				if(event.getX() < headerCell.getComponent().getOffsetWidth()-10) return;
				activeSizer = headerCell;
				oldColumnWidth = getHeaderCell(activeSizer.getColumnIndex()).getWidth();			
			}
		});
		
		headerCellList.add(headerCell);
	}

	protected abstract ISizer createSizer(int columnIndex);
	
	@Override
	public IHeaderCell getHeaderCell(int index) {		
		return headerCellList.get(index);
	}

	@Override
	public void setHeaderListener(IHeaderListener listener) {
		this.listener = listener;
	}

	@Override
	public Widget getComponent() {
		return this;
	}
	
	@Override
	public int getHeaderCellCount() {
		return headerCellList.size();
	}
	
}
